package com.zutassociation.web.controller.system;

import com.zutassociation.common.core.controller.BaseController;
import com.zutassociation.common.core.domain.AjaxResult;
import com.zutassociation.common.core.page.TableDataInfo;
import com.zutassociation.common.utils.SecurityUtils;
import com.zutassociation.system.domain.Message;
import com.zutassociation.system.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息 信息操作处理
 *
 * @author 黄玉东
 * @version 1.0
 * @date 2021/4/20 21:13
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取消息列表
     *
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/listBD")
    public TableDataInfo listBD(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageListBD(message);
        return getDataTable(list);
    }

    /**
     * 根据发送人消息编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/from/{fromId}")
    public AjaxResult getFromInfo(@PathVariable("fromId") Long fromId) {
        return AjaxResult.success(messageService.selectMessageByFromId(fromId));
    }
    /**
     * 根据接收人消息编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/to/{toId}")
    public AjaxResult getToInfo(@PathVariable("toId") Long toId) {
        return AjaxResult.success(messageService.selectMessageByToId(toId));
    }

    /**
     * 根据消息编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId) {
        return AjaxResult.success(messageService.selectMessageById(messageId));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Message message) {
        message.setCreateBy(SecurityUtils.getUsername());
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Message message) {
        message.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 修改消息阅读状态
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PutMapping("/updateReadStatus")
    public AjaxResult updateClick(@Validated @RequestBody Message message) {
        message.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(messageService.updateMessageRead(message));
    }
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@Validated @RequestBody Message message) {
        message.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(messageService.updateMessageStatus(message));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds) {
        return toAjax(messageService.deleteMessageByIds(messageIds));
    }

    /**
     * 批量阅读消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @DeleteMapping("/readStatus0/{messageIds}")
    public AjaxResult deleteMessageByReadStatusIds0(@PathVariable Long[] messageIds) {
        return toAjax(messageService.deleteMessageByReadStatusIds0(messageIds));
    }
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @DeleteMapping("/readStatus1/{messageIds}")
    public AjaxResult deleteMessageByReadStatusIds1(@PathVariable Long[] messageIds) {
        return toAjax(messageService.deleteMessageByReadStatusIds1(messageIds));
    }

}
